package com.e0box.game.logicGraphics.nativeControl
{
	import com.adobe.ane.gameCenter.GameCenterAuthenticationEvent;
	import com.adobe.ane.gameCenter.GameCenterController;
	import com.adobe.ane.gameCenter.GameCenterLeaderboardEvent;
	import com.e0box.game.logicGraphics.Game;
	import com.e0box.game.logicGraphics.data.PlayerData;

	public class GameCenterManager
	{
		
		/*public static const LEADERBOARD_NORMAL:String = "stateNormal";
		public static const LEADERBOARD_TIME:String = "stateTime";
		public static const LEADERBOARD_SURVIVE:String = "stateSurvive";*/
		
		//leaderboard id
		//stateNormal
		//stateTime
		//stateSurvive
		
		private static var _gc:GameCenterController;
		public function GameCenterManager()
		{
			throw new Error("Cant instance the static class 'GameCenterManager'.");
		}
		private static var _ready:Boolean;
		private static function init():void{
			_gc = new GameCenterController();
			
			_gc.addEventListener(GameCenterAuthenticationEvent.PLAYER_AUTHENTICATED, onAuth);
			_gc.addEventListener(GameCenterAuthenticationEvent.PLAYER_AUTHENTICATION_CHANGED, onAuthChange);
			_gc.addEventListener(GameCenterAuthenticationEvent.PLAYER_NOT_AUTHENTICATED, onAuthNot);
			
			_gc.addEventListener(GameCenterLeaderboardEvent.LEADERBOARD_VIEW_FINISHED, onBoardViewFinished);
			
			_gc.addEventListener(GameCenterLeaderboardEvent.SUBMIT_SCORE_FAILED, onSubmitScoreFailed);
			_gc.addEventListener(GameCenterLeaderboardEvent.SUBMIT_SCORE_SUCCEEDED, onSubmitScoreSuccess);
			
			//trace(PlayerData.instance.gameCenterCount,"aaaaaaaaa");
			if(!_gc.authenticated){
				if(PlayerData.instance.gameCenterCount > 10){
					PlayerData.instance.gameCenterCount = 0;
					_gc.authenticate();
				}else{
					PlayerData.instance.gameCenterCount++;
				}
				//trace(PlayerData.instance.gameCenterCount,"aaaaaaaaa");
			}else{
				_ready = true;
			}
		}
		
		protected static function onSubmitScoreSuccess(evt:GameCenterLeaderboardEvent):void
		{
			//
			trace(evt);
			_currentScores.isSubmit = 1;
			
			_currentScores = null;
			
			exeQueue();
		}
		
		protected static function onSubmitScoreFailed(evt:GameCenterLeaderboardEvent):void
		{
			//
			trace(evt);
			
			_currentScores.isSubmit = 0;
			
			_currentScores = null;
			
			exeQueue();
		}
		
		protected static function onBoardViewFinished(evt:GameCenterLeaderboardEvent):void
		{
			//
			trace(evt);
			Game.instance.removeMask();
		}
		
		protected static function onAuthNot(evt:GameCenterAuthenticationEvent):void
		{
			//
			trace(evt);
			
			//suggest user bind game center here
			PlayerData.instance.gameCenterCount = 0;
			
		}
		
		protected static function onAuthChange(evt:GameCenterAuthenticationEvent):void
		{
			//
			trace(evt);
		}
		
		protected static function onAuth(evt:GameCenterAuthenticationEvent):void
		{
			//
			trace(evt);
			_ready = true;
			PlayerData.instance.gameCenterCount = 11;
		}
		
		
		public static function viewLeaderboard(category:String = null):void{
			_gc.showLeaderboardView(category);
			Game.instance.showMask(true);
		}
		
		public static function flushAllScores():void{
			
			var s:ScoreData;
			var i:String;
			
			for(i in PlayerData.instance.normalTop10){
				s = PlayerData.instance.normalTop10[i];
				
				submitScore(s);
			}
			
			for(i in PlayerData.instance.timeTop10){
				s = PlayerData.instance.timeTop10[i];
				
				submitScore(s);
			}
			
			for(i in PlayerData.instance.surviveTop10){
				s = PlayerData.instance.surviveTop10[i];
				
				submitScore(s);
			}
		}
		
		private static var _queue:Vector.<ScoreData> = new Vector.<ScoreData>();;
		private static var _currentScores:ScoreData;
		
		public static function submitScore(score:ScoreData):void{
			
			if(score.isSubmit) return;
			
			_queue.push(score);
			
			exeQueue();
		}
		
		private static function exeQueue():void{
			if(_queue.length && !_currentScores){
				_currentScores = _queue.shift();
				
				if(_currentScores.isSubmit){
					_currentScores = null;
					exeQueue();
					return;
				}else{
					_gc.submitScore(_currentScores.score,_currentScores.category,_currentScores.time);
				}
			}else if(!_queue.length){
				PlayerData.instance.save();
			}
		}
		
		public static function isSubmiting():Boolean{
			return (!_currentScores);
		}
		
		public static function isSupport():Boolean{
			
			return GameCenterController.isSupported;
		}
		
		public static function get controller():GameCenterController
		{
			if(!_gc) init();
			
			return _gc;
		}
		
		public static function get ready():Boolean{
			if(!_gc) init();
			
			return _ready;
		}
	}
}